# -*- coding:utf-8 -*-
#2022/1/16 9:16
# author:xwl

import backend.app.app.models.db_owi_public_models as md_pub
from backend.app.app.db.session_public import session_pub

def well_input_add(datas):
    try:
        save_success=list()
        for data in datas:
            dvpUnit_name=session_pub.query(
                md_pub.TbWellbasic.Belong_DvpUnit_Name
            ).filter(md_pub.TbWellbasic.Well_Name == data['Well_Name']).first()
            if dvpUnit_name:
                dvpUnit_name=dvpUnit_name[0]
                well_inf=session_pub.query(
                    md_pub.TbOilwellstimResult
                ).filter(
                    md_pub.TbOilwellstimResult.Well_Name==data['Well_Name'],
                    md_pub.TbOilwellstimResult.Stim_Date==data['Stim_Date'],
                    md_pub.TbOilwellstimResult.StimType_Name==data['StimType_Name']
                ).first()
                if well_inf:
                    well_inf.Stim_ValidPeriod=data['Stim_ValidPeriod'],
                    well_inf.Stim_IncreOil=data['Stim_IncreOil'],
                    well_inf.Remark=data['Remark']
                else:
                    session_pub.add(
                    md_pub.TbOilwellstimResult(
                        Well_Name=data['Well_Name'],
                        Stim_Date=data['Stim_Date'],
                        StimType_Name=data['StimType_Name'],
                        Stim_ValidPeriod=data['Stim_ValidPeriod'],
                        Stim_IncreOil=data['Stim_IncreOil'],
                        Remark=data['Remark']
                        )
                    )
                session_pub.commit()
                cost_inf=session_pub.query(
                    md_pub.TbYearDvpunitOilstimCost
                ).filter(
                    md_pub.TbYearDvpunitOilstimCost.DvpUnit_Name==dvpUnit_name,
                    md_pub.TbYearDvpunitOilstimCost.Year==data['Stim_Date'][:4],
                    md_pub.TbYearDvpunitOilstimCost.StimType_Name==data['StimType_Name']
                ).first()
                if cost_inf:
                    cost_inf.Stim_Cost=data['Stim_Cost'],
                    cost_inf.Remark= data['Remark']
                else:
                    session_pub.add(md_pub.TbYearDvpunitOilstimCost(
                        DvpUnit_Name = dvpUnit_name,
                        Year = data['Stim_Date'][:4],
                        StimType_Name = data['StimType_Name'],
                        Stim_Cost = data['Stim_Cost'],
                        Remark = data['Remark']
                    ))
                session_pub.commit()

                oilprice_inf = session_pub.query(
                    md_pub.TbYearDvpunitOilprice
                ).filter(
                    md_pub.TbYearDvpunitOilprice.DvpUnit_Name == dvpUnit_name,
                    md_pub.TbYearDvpunitOilprice.Year == data['Stim_Date'][:4],
                ).first()
                if oilprice_inf:
                    oilprice_inf.Oil_CommodityRatio=data['Oil_CommodityRatio'],
                    oilprice_inf.Oil_Price= data['Oil_Price'],
                    oilprice_inf.Remark= data['Remark']
                else:
                    session_pub.add(md_pub.TbYearDvpunitOilprice(
                        DvpUnit_Name=dvpUnit_name,
                        Year=data['Stim_Date'][:4],
                        Oil_CommodityRatio=data['Oil_CommodityRatio'],
                        Oil_Price=data['Oil_Price'],
                        Remark=data['Remark']
                    ))
                session_pub.commit()

                discount_inf = session_pub.query(
                    md_pub.TbYearDvpunitDiscount
                ).filter(
                    md_pub.TbYearDvpunitDiscount.DvpUnit_Name == dvpUnit_name,
                    md_pub.TbYearDvpunitDiscount.Year == data['Stim_Date'][:4],
                ).first()
                if discount_inf:
                    discount_inf.Money_Discount= data['Money_Discount'],
                    discount_inf.Remark= data['Remark']
                else:
                    session_pub.add(md_pub.TbYearDvpunitDiscount(
                        DvpUnit_Name=dvpUnit_name,
                        Year=data['Stim_Date'][:4],
                        Money_Discount=data['Money_Discount'],
                        Remark=data['Remark']
                    ))
                session_pub.commit()
            save_success.append({
                'Well_Name' : data['Well_Name'],
                'DvpUnit_Name' : dvpUnit_name,
                'Stim_Date': data['Stim_Date'],
                'StimType_Name' : data['StimType_Name'],
                'result':True
            })
        return save_success
    except Exception as e:
        print(e)
        session_pub.rollback()
        return False